package com.slg.android.listenbook.biz.bookcity.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import com.slg.android.listenbook.R;
import com.slg.android.listenbook.biz.bookcity.CategoryDetailActivity;
import com.slg.android.listenbook.biz.home.MainActivity;

import java.util.HashMap;
import java.util.LinkedList;

public class CategoryFragment extends Fragment implements AdapterView.OnItemClickListener {


    private GridView gridView;
    private LinkedList<HashMap<String, Object>> data;
    private String[] type;
    public CategoryFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View ret = inflater.inflate(R.layout.fragment_category, container, false);
        gridView = (GridView) ret.findViewById(R.id.category_fragment_grid_view);
        if(gridView!= null){
            data = new LinkedList<HashMap<String,Object>>();
            HashMap<String,Object> map = new HashMap<String,Object>();
            map.put("icon", R.drawable.icon_type_xs);
            map.put("title","有声小说");
            data.add(map);

            map = new HashMap<String,Object>();
            map.put("icon", R.drawable.icon_type_ps);
            map.put("title","相声评书");
            data.add(map);

            map = new HashMap<String,Object>();
            map.put("icon", R.drawable.icon_type_jy);
            map.put("title","心灵鸡汤");
            data.add(map);

            map = new HashMap<String,Object>();
            map.put("icon", R.drawable.icon_type_yl);
            map.put("title","综艺娱乐");
            data.add(map);

            map = new HashMap<String,Object>();
            map.put("icon", R.drawable.icon_type_jq);
            map.put("title","火热金曲");
            data.add(map);

            map = new HashMap<String,Object>();
            map.put("icon", R.drawable.icon_type_sh);
            map.put("title","人文生活");
            data.add(map);

            map = new HashMap<String,Object>();
            map.put("icon", R.drawable.icon_type_mz);
            map.put("title","文学名著");
            data.add(map);

            map = new HashMap<String,Object>();
            map.put("icon", R.drawable.icon_type_gb);
            map.put("title","广播剧场");
            data.add(map);

            map = new HashMap<String,Object>();
            map.put("icon",R.drawable.icon_type_sy);
            map.put("title","商业时事");
            data.add(map);

            type = new String[]{"1","3","104",
                                "54","","",
                                "78","",""
            };

            SimpleAdapter adapter = new SimpleAdapter(
                    getActivity(),
                    data,
                    R.layout.item_category_fragment_grid_view,
                    new String[]{"icon","title"},
                    new int[]{R.id.item_category_fragment_img,R.id.item_category_fragment_txt}
                    );

            gridView.setAdapter(adapter);
            gridView.setOnItemClickListener(this);
        }

        return ret;
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        /*if(position ==7||position==8){

        }else */if(position == 4){
            Intent intent = new Intent(getActivity(), MainActivity.class);
            startActivity(intent);
        }else {
            HashMap<String, Object> titleName = data.get(position);
            Object title = titleName.get("title");
            String titleTxt = null;
            if (title instanceof String) {
                titleTxt = (String) title;
            }

            Intent intent = new Intent(getActivity(), CategoryDetailActivity.class);
            intent.putExtra("title", titleTxt);
            String typeTxt = type[position];
            intent.putExtra("type", typeTxt);

            startActivity(intent);

        }

    }
}
